package com.example.bbs.controller;

import com.example.bbs.dto.UserInfo;
import com.example.bbs.mapper.UserInfoMapper;
import com.example.bbs.pojo.Result;
import com.example.bbs.utils.ThreadLocalUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class UserInfoController {
    @Autowired
    private UserInfoMapper userInfoMapper;
    //查询用户信息
    @GetMapping("/userinfo")
    public Result selectUserInfoData(){
        //获取用户名
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        return Result.success(userInfoMapper.selectUserInfoData(username));
    }
    @PostMapping("/userinfo")
    public Result update(@JsonFormat UserInfo userInfo){
//        System.out.println(userInfo);
        //获取用户名
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        userInfo.setUsername(username);
        userInfoMapper.updateUserInfo(userInfo);
        return Result.success();
    }

}
